package cn.xshi.monitor.web;

import cn.xshi.monitor.service.IotVideoGroupService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.base.BaseSearch;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.monitor.dao.IotVideoGroupRightDao;
import cn.xshi.monitor.form.IotVideoGroupRightForm;
import cn.xshi.monitor.model.IotVideoGroup;
import cn.xshi.monitor.model.IotVideoGroupRight;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 相机组
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/iotVideoGroup")
@Api(value = "相机组",tags = "相机组", description = "相机组")
public class IotVideoGroupController  extends BaseAction {

    @Resource
    IotVideoGroupService iotVideoGroupService;

    @Resource
    IotVideoGroupRightDao iotVideoGroupRightDao;

    /**
     * 查询并分页
     * @param baseSearch
     */
    @ApiOperation(value="查询并分页", notes="查询并分页")
    @PostMapping(value="/list")
    @Auth(value = "/iotVideoGroup/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BasePage<List<IotVideoGroup>> getIotVideoListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        commonHPager(baseSearch);
        List<IotVideoGroup> iotVideoGroups = iotVideoGroupService.getIotVideoGroupByCondition(condition);
        for(IotVideoGroup iotVideoGroup:iotVideoGroups){
            if(!StringUtil.isEmpty(iotVideoGroup.getCreateId())){
                OauthAccountEntity createBy = getAccount(iotVideoGroup.getCreateId());
                if(null != createBy){
                    iotVideoGroup.setCreateBy(createBy.getName());
                }
            }
            if(!StringUtil.isEmpty(iotVideoGroup.getUpdateId())){
                OauthAccountEntity modifiedBy = getAccount(iotVideoGroup.getUpdateId());
                if(null != modifiedBy){
                    iotVideoGroup.setModifiedBy(modifiedBy.getName());
                }
            }
        }
        PageInfo<IotVideoGroup> page = new PageInfo<IotVideoGroup>(iotVideoGroups);
        return outPageBootStr(page,baseSearch);
    }

    /**
     * 查询单条记录
     * @param id
     */
    @ApiOperation(value="查询单条记录", notes="查询单条记录")
    @GetMapping(value="/get/{id}")
    @Auth(value = "/get/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<IotVideoGroup> getIotVideoGroupById(@PathVariable("id")String id) {
        IotVideoGroup iotVideoGroup = iotVideoGroupService.getIotVideoGroupById(id);
        if (!StringUtil.isEmpty(iotVideoGroup.getCreateId())) {
            OauthAccountEntity createBy = getAccount(iotVideoGroup.getCreateId());
            if (null != createBy) {
                iotVideoGroup.setCreateBy(createBy.getName());
            }
        }
        if (!StringUtil.isEmpty(iotVideoGroup.getUpdateId())) {
            OauthAccountEntity modifiedBy = getAccount(iotVideoGroup.getUpdateId());
            if (null != modifiedBy) {
                iotVideoGroup.setModifiedBy(modifiedBy.getName());
            }
        }
        return outDataStr(iotVideoGroup);
    }

    /**
     * 添加
     * @param iotVideoGroup
     */
    @ApiOperation(value="添加", notes="添加")
    @PostMapping(value="/add")
    public BaseResult addIotVideoGroup(@RequestBody IotVideoGroup iotVideoGroup){
        int i = 0;
        if(null != iotVideoGroup){
            i=iotVideoGroupService.addIotVideoGroup(iotVideoGroup);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 修改
     * @param iotVideoGroup
     */
    @ApiOperation(value="修改", notes="修改")
    @PutMapping(value="/update")
    public BaseResult updateIotVideoGroup(@RequestBody IotVideoGroup iotVideoGroup){
        int i = 0;
        if(null != iotVideoGroup){
            i=iotVideoGroupService.updateIotVideoGroup(iotVideoGroup);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 删除
     * @param id
     */
    @ApiOperation(value="删除", notes="删除")
    @DeleteMapping(value="/delete")
    public BaseResult delIotVideoGroup(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=iotVideoGroupService.delIotVideoGroup(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 查询已配置相机
     * @param baseSearch
     */
    @ApiOperation(value="查询已配置相机", notes="查询已配置相机")
    @PostMapping(value="/rightList")
    @Auth(value = "/get/rightList",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
    public BaseResult<List<IotVideoGroupRight>> getIotVideoRightListByCondition(@RequestBody(required=true)BaseSearch baseSearch){
        Map<String, Object> condition = baseSearch.convert();
        List<IotVideoGroupRight> iotVideoGroupRights = iotVideoGroupRightDao.getIotVideoGroupRightByCondition(condition);
        return new BaseResult(iotVideoGroupRights);
    }

    /**
     * 删除单个摄像头
     * @param id
     */
    @ApiOperation(value="删除单个摄像头", notes="删除单个摄像头")
    @DeleteMapping(value="/remove")
    public BaseResult delIotVideoGroupRight(String id){
        int i = 0;
        if(!StringUtil.isEmpty(id)){
            Map<String, Object> condition = new HashMap<String, Object>();
            condition.put("id",id.split(","));
            i=iotVideoGroupRightDao.delIotVideoGroupRight(condition);
        }
        if(i>0){
            return outAudStr(true);
        }else{
            return outAudStr(false);
        }
    }

    /**
     * 添加组相机
     * @return
     */
    @ApiOperation(value="添加组相机", notes="添加组相机")
    @PostMapping(value="/addRight")
    public BaseResult addRight(@RequestBody IotVideoGroupRightForm iotVideoGroupRightForm){
        return iotVideoGroupService.addRight(iotVideoGroupRightForm);
    }
}
